﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using Frame.Lib.Data;
using Frame.Lib.Utilities;
using Frame.Lib.Common;


public partial class Admin_Tools_ImportData : System.Web.UI.Page
{
    private DBHelper dbOper;

    protected void Page_Load(object sender, EventArgs e)
    {
        JSUtil.SetDivHeight(txtContent.ClientID, 130);
        if (!IsPostBack)
        {
            txtConectionString.Text = AppSetting.ConnectionString;
        }

        dbOper = DBHelper.Instance(txtConectionString.Text);
    }

    protected void btnOK_Click(object sender, EventArgs e)
    {
        try
        {
            string strSelectSql = txtSql.Text;
            StringBuilder sbInsertSql = new StringBuilder();

            if (strSelectSql == "")
            {
                string[] strTableNames = txtTableName.Text.Split(',');

                foreach (string tableName in strTableNames)
                {
                    strSelectSql = "select * from [" + tableName.TrimStart('[').TrimEnd(']') + "]";
                    SetInsertSql(sbInsertSql, tableName, strSelectSql);
                    sbInsertSql.Append("\n");
                }
            }
            else
            {
                SetInsertSql(sbInsertSql, txtTableName.Text, strSelectSql);
            }

            txtContent.Text = sbInsertSql.ToString();
        }
        catch
        {
            JSUtil.Alert("数据库连接错误或表名不存在");
        }
    }
    private void SetInsertSql(StringBuilder sbInsertSql, string strTableName, string strSelectSql)
    {
        strTableName = "[" + strTableName.TrimStart('[').TrimEnd(']') + "]";
        DataTable dt = dbOper.Query(strSelectSql);
        DataColumnCollection cols = dt.Columns;
        sbInsertSql.Append("set identity_insert " + strTableName + " on;\n");
        foreach (DataRow dr in dt.Rows)
        {
            string strFields = "";
            string strValues = "";
            foreach (DataColumn col in cols)
            {
                strFields += ",[" + col.ColumnName + "]";
                if (dr[col.ColumnName] is DBNull)
                {
                    strValues += ",null";
                }
                else
                {
                    if (IsString(col))
                    {
                        strValues += ",'" + dr[col.ColumnName].ToString().Replace("'","''") + "'";
                    }
                    else
                    {
                        if (dr[col.ColumnName] is bool)
                        {
                            strValues += "," + Convert.ToInt16(dr[col.ColumnName]);
                        }
                        else
                        {
                            strValues += "," + dr[col.ColumnName];
                        }
                    }
                }
            }
            if (strFields != "")
            {
                strFields = " (" + strFields.Substring(1) + ")";
                strValues = " (" + strValues.Substring(1) + ")";
            }
            sbInsertSql.Append("insert into " + strTableName + strFields + " values " + strValues + ";\n");
        }
        sbInsertSql.Append("set identity_insert " + strTableName + " on;\n");
    }

    private bool IsString(DataColumn col)
    {
        Type type = col.DataType;
        bool blnFlag = true;
        if (type == typeof(int) || type == typeof(float) || type == typeof(bool) || type == typeof(double) || type == typeof(decimal))
        {
            blnFlag = false;
        }
        return blnFlag;
    }
}
